package com.example.kliven.because.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.kliven.because.R;
import com.example.kliven.because.activity.BreadActivity;
import com.example.kliven.because.activity.LoginActivity;
import com.example.kliven.because.utils.DataCleanManager;
import com.example.kliven.because.utils.DayNightMode;

public class MeFragment extends Fragment implements View.OnClickListener {

    private DrawerLayout drawerLayout;
    private TextView tv_goBrand;
    private TextView snackbar_action;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.me_fragment,null);

        drawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_Layout);
        view.findViewById(R.id.btn_setting).setOnClickListener(this);
        view.findViewById(R.id.btn_login).setOnClickListener(this);
        view.findViewById(R.id.btn_sleep).setOnClickListener(this);
        snackbar_action = (TextView) view.findViewById(R.id.snackbar_action);
        drawerLayout.setOnClickListener(this);
        view.findViewById(R.id.clean_phone).setOnClickListener(this);
        tv_goBrand = (TextView) view.findViewById(R.id.go_brand);
        tv_goBrand.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View view) {
        int theme = DayNightMode.getDayNightMode(getActivity());
        switch (view.getId()){
            case R.id.btn_setting:
                drawerLayout.openDrawer(Gravity.RIGHT);
                break;
            case R.id.clean_phone:
                DataCleanManager.cleanDatabases(getActivity());
                DataCleanManager.cleanExternalCache(getActivity());
                DataCleanManager.cleanFiles(getActivity());
                DataCleanManager.cleanInternalCache(getActivity());
                DataCleanManager.cleanSharedPreference(getActivity());
                Log.e("MeFragment", "清理缓存");
                Snackbar.make(snackbar_action,"缓存清理完成",Snackbar.LENGTH_SHORT).show();
                break;
            case R.id.go_brand:
                openWebPage();
                break;
            case R.id.btn_login:
                Intent loginIntent = new Intent(getActivity(), LoginActivity.class);
                startActivity(loginIntent);
                getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                break;
            case R.id.btn_sleep:
                if (theme == DayNightMode.THEME_SUN) {
                    DayNightMode.setDayNightMode(getActivity(),DayNightMode.THEME_NIGHT);
                } else {
                    DayNightMode.setDayNightMode(getActivity(),DayNightMode.THEME_SUN);
                }
                // 注意改过主题后一定要，把activity finish在重开一遍，因为更改主题只能在oncreat中进行
                getActivity().finish();
                getActivity().startActivity(new Intent(getActivity(),getActivity().getClass()));
                break;
        }
    }

    private void openWebPage() {
        Intent intent = new Intent(getActivity(), BreadActivity.class);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
    }
}
